package com.prs.crm.action.oa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;


import com.prs.crm.domain.oa.KnowledgeType;
import com.prs.crm.service.oa.KnowledgeTypeService;
import com.prs.crm.service.sys.LogService;

public class KnowledgeTypeAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private KnowledgeType knowledgeType;
	private KnowledgeTypeService knowledgeTypeService;
	private PaginationSupport pagination;
	private Integer[] knowledgeTypeIds;
	private Collection<TreeNode> nodes;
	private LogService logService;

	@Action("listKnowledgeType")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(KnowledgeType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getKnowledgeTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action(value="loadKnowledgeTypeTree",results={@Result(name="success",type="json",params={"root","nodes"})})
	public String loadKnowledgeTypeTree(){
		nodes = new ArrayList<TreeNode>();
		List<KnowledgeType> regs = this.getKnowledgeTypeService().getAllKnowledgeType();
		for(KnowledgeType reg : regs) {
			TreeNode node =new TreeNode();
			node.setId(reg.getId().toString());
			node.setText(reg.getName());
			nodes.add(node);
		}
		return SUCCESS;
	}
	
	
	@Action("selectKnowledgeType")
	public String selectKnowledgeType() {
		return this.execute();
	}
	
	@Action("editKnowledgeType")
	public String edit() {
		return SUCCESS;
	}
	
	@Action(value="saveKnowledgeType",results={@Result(name="success",type="redirect",location="listKnowledgeType"),
			@Result(name="error",type="chain",location="editKnowledgeType")})
	public String save() {
		if(knowledgeType.getName()==null||"".equals(knowledgeType.getName().trim())){
			addActionError("类型不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(KnowledgeType.class, knowledgeType
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getKnowledgeTypeService().save(knowledgeType);
		return SUCCESS;
	}
	
	@Action(value="removeKnowledgeType",results={@Result(name="success",type="redirect",location="listKnowledgeType"),
			@Result(name="error",type="chain",location="listKnowledgeType")})
	public String remove() {
		if(knowledgeTypeIds == null) {
			this.addActionError("请选择要删除的知识类型");
			return ERROR;
		}
		for(Integer id : knowledgeTypeIds) {
			KnowledgeType type = this.getKnowledgeTypeService().get(id);
			if(type.getKnowledges().size() != 0) {
				this.addActionError("已有知识引用，不能删除");
				return ERROR;
			}
		}
		this.getKnowledgeTypeService().batchRemove(knowledgeTypeIds);
		return SUCCESS;
	}
	
	public KnowledgeType getKnowledgeType() {
		return knowledgeType;
	}
	public void setKnowledgeType(KnowledgeType knowledgeType) {
		this.knowledgeType = knowledgeType;
	}
	public KnowledgeTypeService getKnowledgeTypeService() {
		return knowledgeTypeService;
	}
	public void setKnowledgeTypeService(KnowledgeTypeService knowledgeTypeService) {
		this.knowledgeTypeService = knowledgeTypeService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getKnowledgeTypeIds() {
		return knowledgeTypeIds;
	}
	public void setKnowledgeTypeIds(Integer[] knowledgeTypeIds) {
		this.knowledgeTypeIds = knowledgeTypeIds;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
